package com.example.springboot1.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.util.PoitlIOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ckj
 * @description TODO
 * @date 2022-12-05 14:04
 */

@RestController
public class WordTest {

    public static void main(String[] args) throws IOException {
//        WordTest wordTest=new WordTest();
//        wordTest.test();
/*        String filePath = "D:\\template.docx";
        String targetPath =  "D:\\textTestTarget.docx";
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(
                new HashMap<String, Object>() {
                    {
                        put("title", "测试");
                        put("author", Texts.of("Liziba").color("000000").create());
                        put("link", Texts.of("百度").link("https://baidu.com").create());
                        put("anchor", Texts.of("anchortxt").anchor("appendix1").create());
                    }
                });
        template.writeAndClose(new FileOutputStream(targetPath));*/


/*        String filePath = "D:\\asjd.docx";
        String targetPath = "D:\\pictureTest3.docx";
        String picPath = "http://140.246.86.11:9093/group1/M00/00/0C/rB0AKmNxBJaAVo-KAA1LWx2aTUs74.jpeg";
//        String picPath =  "D:\\新.jpg";
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(
                new HashMap<String, Object>() {
                    {
                        // 方法一、图片路径（原大小）
//                        put("picture", picPath);
                        // 方法二、指定图片大小
//                        put("picture", Pictures.ofLocal(picPath).size(420,350).center().create());
                        // 方法三、图片流
//                        put("picture", Pictures.ofStream(new FileInputStream(picPath), PictureType.JPEG)
//                                .size(420,350).create());
                        // 针对网络图片、SVG图片、Java图片都有处理
                        put("picture", Pictures.ofUrl(picPath)
                                .size(100, 100).create());
                    }
                });
        template.writeAndClose(new FileOutputStream(targetPath));*/

        System.out.println("✓ 一级故障 \t □ 二级故障");
    }

    void test() throws IOException {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("template.docx");
        String PICTURE_TAG_NAME = "pic";
//        Configure config = Configure.builder().bind(PICTURE_TAG_NAME, new DocumentRenderPolicy()).build();
        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream);
        template.render(
                new HashMap<String, Object>() {{
                    put("title", "Hi, poi-tl Word模板引擎");
                }});
        template.writeAndClose(new FileOutputStream("output.docx"));
    }

    @GetMapping("/word")
    public void word(HttpServletResponse response) throws IOException {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("template.docx");
        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream);
        String picPath = "http://140.246.86.11:9093/group1/M00/00/0C/rB0AKmNxBJaAVo-KAA1LWx2aTUs74.jpeg";
        template.render(
                new HashMap<String, Object>() {
                    {
                        put("picture", Pictures.ofUrl(picPath).size(100, 100).create());
                    }
                });
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=\"" + "out_template.docx" + "\"");
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);
    }


    public void rich(HttpServletResponse response) throws IOException {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("template.docx");
        XWPFTemplate template = XWPFTemplate.compile(resourceAsStream);
        String picPath = "http://140.246.86.11:9093/group1/M00/00/0C/rB0AKmNxBJaAVo-KAA1LWx2aTUs74.jpeg";
        template.render(
                new HashMap<String, Object>() {
                    {
                        put("picture", Pictures.ofUrl(picPath).size(100, 100).create());
                    }
                });
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=\"" + "out_template.docx" + "\"");
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);
    }

    @GetMapping("/rich")
    public void test(HttpServletResponse response) {
        //数据源

        try {
            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("templates/template.docx");
             Map<String, Object> data = new HashMap<>();

//            data.put("text","<h1>This is a heading</h1><p>This is a paragraph.</p><ul><li>Item 1</li><li>Item 2</li></ul>");
            data.put("text", "<h1>text22</h1>");
//            //这个，就是我们导出html格式文件需需要的模板渲染
//            HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
//            //将存储的html字段进行渲染
//            Configure configure = Configure.builder()
//                    .bind("text", htmlRenderPolicy)
//                    .build();
            XWPFTemplate template = XWPFTemplate.compile(resourceAsStream).render(data);
            String fileName = "测试导出富文本框.docx";
            //这里是通过浏览器导出，当然，也可导出到本地磁盘里。
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" +
                    URLEncoder.encode(fileName, "UTF-8"));
            ServletOutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            template.write(bos);




            bos.flush();
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
            out.close();
            bos.close();
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
